// miniprogram/pages/applications/addaddress/addaddress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    region: ['北京市', '北京市', '东城区'],
    customItem: '全部',
    area: '',
    isdefault: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  bindRegionChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let val = e.detail.value;
    this.setData({
      region: e.detail.value,
      province: val[0],
      city: val[1],
      area: val[2],
      aarea: val[0] + '' + val[1] + '' + val[2],
    })
  },
  isDefaultAddress() {
    var isdefault = !this.data.isdefault;
    this.setData({
      isdefault: isdefault
    })
  },
  syncWxAddress() {
    let _this = this;
    let db = wx.cloud.database();
    wx.chooseAddress({
      success: res => {
        console.log(res)
        _this.setData({
          name: res.userName,
          telephone: res.telNumber,
          detail: res.detailInfo,
          city: res.cityName,
          province: res.provinceName,
          area: res.countyName,
          aarea: res.provinceName + '' + res.cityName + '' + res.countyName,
          is_default: false,
        })
      }
    })

  },
  getName(e) {
    var val = e.detail.value;
    this.setData({
      name: val
    })
  },
  getTelephone(e) {
    var val = e.detail.value;
    this.setData({
      telephone: val
    })
  },
  getDetailAddress(e) {
    var val = e.detail.value;
    this.setData({
      address: val
    })
  },
  getFormSubmit(e) {
    console.log(e)
    var data = e.detail.value;
    for (let i in data) {
      if (!data[i]) {
        wx.showToast({
          title: '请填写完整',
        })
        return false;
      }
    }
    data.isdefault = this.data.isdefault;
    data.province = this.data.province;
    data.city = this.data.city;
    data.area = this.data.area;
    console.log(data);
    this.onAddAdress(data);
  },
  onAddAdress(data) {
    let db = wx.cloud.database();
    let _this = this;
    if (data.isdefault) {
      db.collection('tb_address_list').where({
        isdefault: true
      }).get({
        success: res => {
          console.log(res)
          if (res.data.length > 0) {
            let id = res.data[0]._id;
            wx.cloud.callFunction({
              name: 'updateDefaultAddress',
              data: {
                id: id
              },
              success: res => {
                console.log(res)
                if(res.result.stats.updated==1){
                  db.collection('tb_address_list').add({
                    data: data,
                    success: res => {
                      console.log(res)
                      wx.showToast({
                        title: '添加成功',
                        success:res=>{
                          let page=getCurrentPages();
                          if (page[page.length - 2] == 'null' && page[page.length - 2]=='undefined'){
                            return false;
                          }
                          page[page.length - 2].onLoad();
                          wx.navigateBack({
                            delta:1,
                          })
                        }
                      })
                    },
                    fail: err => {
                      console.log(err)
                    }
                  })
                }
              },
              fail: err => {
                console.log(err)
              }
            })
          }
        },
        fail: err => {
          console.log(err)
        }
      });
    }else{
      db.collection('tb_address_list').add({
        data: data,
        success: res => {
          console.log(res)
          wx.showToast({
            title: '',
          })
        },
        fail: err => {
          console.log(err)
        }
      })
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})